﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FSViet.Survey.Helpers;
using System.Data;
using System.Web.UI.HtmlControls;

namespace FSViet.Survey.Administration.Campaign
{
	public partial class List : ListPageBase
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			CheckSite();

			if (!IsPostBack)
			{
				int page = 1;

				try
				{
					if (Request.QueryString["p"] != null)
						page = Convert.ToInt32(Request.QueryString["p"]);
				}
				catch
				{
				}

				if (SiteId != Guid.Empty)
				{
					LoadData(page, SiteId);
				}
			}
		}

		void LoadData(int page, Guid siteId)
		{
			page = page - 1;
			if (page < 0) page = 0;
			try
			{
				DataHelper dataH = new DataHelper();
				int totalRecord = 0;
				var data = dataH.Load(
					"SELECT COUNT(*) FROM Campaigns WHERE SiteId=@SiteId",
					"SELECT * FROM Campaigns WHERE SiteId=@SiteId",
					page * GlobalApplication.ItemPerPage,
					GlobalApplication.ItemPerPage,
					out totalRecord,
					dataH.CreateParam("SiteId", System.Data.SqlDbType.Char, GlobalApplication.GetGuidS(siteId)));

				ShowPage(totalRecord, GlobalApplication.GetGuidS(siteId));

				ListItem.DataSource = data;
				ListItem.DataBind();
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
			}
		}

		void ShowPage(int totalRecord, string siteId)
		{
			if (totalRecord > GlobalApplication.ItemPerPage)
			{ }
			else
			{
				PageList.Visible = false;
				return;
			}
			int totalPage = totalRecord / GlobalApplication.ItemPerPage + 1;
			var ul = new HtmlGenericControl("ol");
			ul.Attributes.Add("class", "pagination");
			for (int i = 1; i <= totalPage; i++)
			{
				var li = new HtmlGenericControl("li");
				li.Controls.Add(new HyperLink() { Text = i.ToString(), NavigateUrl = "?Id=" + siteId + "&p=" + i.ToString() });
				ul.Controls.Add(li);
			}
			PageList.Controls.Add(ul);
		}

		string BuildPramToDetails()
		{
			string result = string.Empty;

			return result;
		}
	}
}